<?php

/**
 * Wget Curl驱动核心
 *
 * @author	 jonwang(jonwang@myqee.com)
 * @category   MyQEE
 * @package	System
 * @subpackage Core
 * @copyright  Copyright (c) 2008-2011 myqee.com
 * @license	http://www.myqee.com/license.html
 */
class MyQEE_HttpGet_Driver_Fsock extends Snoopy
{

    protected $http_data = array();

    /**
     * 设置$cookie
     * @param $agent
     */
    public function set_agent($agent)
    {
        $this->agent = $agent;
    }

    /**
     * 设置$cookie
     * @param string $cookie
     */
    public function set_cookies($cookies)
    {
        $this->cookies = $cookies;
    }

    /**
     * 设置$referer
     * @param string $referer
     */
    public function set_referer($referer)
    {
        $this->referer = $referer;
    }

    public function set_option($key, $value)
    {
        //TODO 暂时不支持
    }

    /**
     * 用POST方式提交
     * @param $url
     * @param string/array $vars
     * @param $timeout 超时时间，默认120秒
     * @return string, false on failure
     */
    public function post($url, $vars, $timeout = 30)
    {
        $this->read_timeout = $timeout;
        $time = microtime(true);
        $this->submit($url, $vars);
        $time = microtime(true) - $time;
        
        $this->http_data = $this->get_data($time);
        
        return $this->results;
    }

    /**
     * GET方式获取数据
     * @param string/array $url
     * @param int $timeout
     * @return string, false on failure
     */
    public function get($url, $timeout = 10)
    {
        if ( is_array($url) )
        {
            return $this->get_urls($url, $timeout);
        }
        
        $this->read_timeout = $timeout;
        $time = microtime(true);
        $this->fetch($url);
        $time = microtime(true) - $time;
        
        $this->http_data = $this->get_data($time);
        return $this->response_code == 200 ? $this->http_data['data'] : false;
    }

    /**
     * 支持多URL请求
     * TODO 目前只是单进程
     * 
     * @param array $urls
     * @param int $timeout
     */
    protected function get_urls($urls, $timeout = 1)
    {
        $http_data = array();
        foreach ( $urls as $url )
        {
            $result[$url] = $this->get($url, $timeout);
            $http_data[$url] = $this->http_data;
        }
        $this->http_data = $http_data;
        
        return $result;
    }

    public function get_resut_data()
    {
        return $this->http_data;
    }

    protected function get_data($time)
    {
        return array('data' => $this->results, 'cookies' => $this->cookies, 'headers' => $this->headers, 'code' => $this->response_code, 'time' => $time);
    }
}